package com.yuchen.ishop.web.mytags;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.yuchen.ishop.entity.SellerXiaoer;
import com.yuchen.ishop.service.CacheService;
import com.yuchen.ishop.service.buyer.BuyerRestClient;
import com.yuchen.ishop.service.goods.GoodsImgRestClient;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.XiaoerRestClient;
import com.yuchen.utils.PrettytimeUtils;
import com.yuchen.utils.SpringContextUtils;
import com.yuchen.utils.config.ConfigUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.yuchen.core.cache.J2CacheUtils;
import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.entity.Seller;
import com.yuchen.utils.security.Base64Utils;

/**
 * @author EL 自定义标签
 *         方法必须是public static的公有静态方法
 */
public class MyEL {

    private final static String[] roles = {"seller", "buyer", "admin", "xiaoer"};

    private final static CacheService cacheService = SpringContextUtils.getBean(CacheService.class);

    private final static SellerRestClient sellerRestClient = SpringContextUtils.getBean(SellerRestClient.class);

    private final static XiaoerRestClient xiaoerRestClient = SpringContextUtils.getBean(XiaoerRestClient.class);

    private final static BuyerRestClient buyerRestClient = SpringContextUtils.getBean(BuyerRestClient.class);

    private final static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final static SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 解码cookie里的ishopid
     *
     * @param ishopid
     * @return
     */
    public static Map decodeIshopid(String ishopid) {
       
        return null;
    }

    public static String formatTimestamp(Long time) {
        if (time > 0l) {
            Date date = new Date(time);
            return sf.format(date);
        }
        return "";
    }

    public static String formatDate(Date date) {
        if (date == null) return "";
        return sf1.format(date);
    }

    public static String goodsMainImgUrl(Long goodsId) {
        String fileServerUrl = ConfigUtils.config.get("fileServerUrl");
        GoodsImgRestClient imgService = SpringContextUtils.getBean(GoodsImgRestClient.class);
        String mainUrl = imgService.getGoodsMainImg(goodsId);
        if (StringUtils.isBlank(mainUrl)) {
            //TODO: 默认一个空图片

        }
        return fileServerUrl + "/" + mainUrl;
    }

    public static String formatPhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return "";
        }
        StringBuilder phoneBuilder = new StringBuilder();
        phoneBuilder.append(StringUtils.substring(phone, 0, 3)).append(" ");
        phoneBuilder.append(StringUtils.substring(phone,3,7)).append(" ");
        phoneBuilder.append(StringUtils.substring(phone,7)).append(" ");
        return phoneBuilder.toString();
    }

    public static String address(Long id) {
        List<Map> addresses = cacheService.getAddressCache();
        if (addresses != null && addresses.size() > 0) {
            for (Map map : addresses) {
                Long idTemp = Long.valueOf(map.get("id").toString());
                if (id.equals(idTemp)) {
                    return (String) map.get("name");
                }
            }
        }
        return "";
    }

    public static boolean isNotBlank(String val) {
        return StringUtils.isNotBlank(val);
    }

    public static String prettyTime(Long date) {
        return PrettytimeUtils.pretty(new Date(date));
    }





}
